package model;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;
import databeans.UserBean;

public class UserDAO {
	// the BeanFactory
	private BeanFactory<UserBean> factory;
	
	public UserDAO() throws DAOException{
		BeanTable<UserBean> userTable = BeanTable.getInstance(UserBean.class,"DroidChat_user");
		
		if (!userTable.exists()) {
			userTable.create("userID");   // userID is the primary key (auto-increment)
		}
		
		factory = userTable.getFactory();
	}
	
	/****
	 * This create() method that creates a new user record in the "user"
	 *  table given a UserBean containing all the fields that describe a
	 *  user, except for the user id, which is generated by the server.
	 *  
	 * It returns a new UserBean that contains a copy of the information
	 *  given to the create() method and contains the server generated 
	 *  user id.
	 *  
	 * @param newUser
	 * @return
	 * @throws DAOException
	 */
	public UserBean create(UserBean newUser) throws DAOException {
		UserBean dbNewUser;
		
		try {
			if(factory.match(MatchArg.equalsIgnoreCase("loginID", newUser.getLoginID())).length!=0)
				throw new DuplicateKeyException("loginID has already been used: "+newUser.getLoginID());
			
			Transaction.begin();
			dbNewUser = factory.create();
			dbNewUser.setLoginID(newUser.getLoginID());
			dbNewUser.setPassword(newUser.getPassword());
			Transaction.commit();
			return dbNewUser;
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
	}
	
	/****
	 * This lookup() method takes a user's login id and
	 *  returns a UserBean containing information about that user.
	 *  
	 *  If no user in the db has this e-mail address, null is returned.
	 *  
	 * @param loginID
	 * @return
	 * @throws DAOException
	 */
	public UserBean lookup(String loginID) throws DAOException{
		UserBean[] arr=null;
		
		try {
			arr = factory.match(MatchArg.equalsIgnoreCase("loginID", loginID));
		} catch (RollbackException e) {
			e.printStackTrace();
		}
		
		if(arr==null){
			return null;
		}else if(arr.length==0){
			return null;
		}
		return arr[0];
	}
	
	/****
	 * This lookupByID() method takes a user's userID and
	 *  returns a UserBean containing information about that user.
	 *  
	 *  If no user in the db has this userID, null is returned.
	 *  
	 * @param loginID
	 * @return
	 * @throws DAOException
	 */
	public UserBean lookupByID(String loginID) throws DAOException{
		UserBean[] arr=null;
		try {
			arr = factory.match(MatchArg.equals("userID", loginID));
		} catch (RollbackException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(arr==null){
			return null;
		}else if(arr.length==0){
			return null;
		}
		return arr[0];
	}
	
}
